package kr.hkit.powerjava.chapter25.chat;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class QuizServer {
	public static void main(String[] args) throws IOException {
		ServerSocket serversocket = null;
		Socket clientSocket = null;
		PrintWriter out = null;
		BufferedReader in = null;
		
		try {
			serversocket = new ServerSocket(5555);
		} catch (IOException e) {
			System.err.println("다음의 포트 번호에 연결할 수 없습니다.");
			System.exit(-1);
		}
		
		
		
		try {
			clientSocket = serversocket.accept();
		} catch (IOException e) {
			System.err.println("accept() fail");
			System.exit(-1);
		}
		
		out = new PrintWriter(clientSocket.getOutputStream(),true);
		in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		
		String inputLine;
		String outputLine;
		
		QuizPrtocol qp = new QuizPrtocol();
		
		outputLine = qp.proceess(null);
		out.println(outputLine);
		
		while((inputLine=in.readLine()) != null){
			outputLine = qp.proceess(inputLine);
			out.println(outputLine);
			if(outputLine.equals("quit"))
				break;
		}
		out.close();
		in.close();
		clientSocket.close();
		serversocket.close();		
	}
}
